<?php

namespace app\company\controller;

use app\common\exception\BaseException;
use app\common\service\annotation\NodeAnnotation;
use app\company\model\CompanyWarehouseLocation;
use app\common\controller\CompanyController;
use app\common\service\annotation\ControllerAnnotation;
use app\company\validate\LocationValidate;
use app\Request;
use think\App;
use think\response\Json;

/**
 * @ControllerAnnotation(title="仓库位置")
 */
class WarehouseLocation extends CompanyController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model         = new CompanyWarehouseLocation();
        $this->validateClass = LocationValidate::class;
        $this->joinTable     = 'area, warehouse';
        $this->each          = true;
    }

    public function each($list)
    {
        return $list->each(function ($item)
        {
            $item->area_name = $item->area->name;
            $item->warehouse_name = $item->warehouse->name;
        });
    }

    /**
     * @NodeAnnotation (title="新增")
     * @throws BaseException
     */
    public function add(Request $request): Json
    {
        $this->checkPost();
        $post = $request->post();
//        $this->sceneValidate($post, 'add');
        $post['company_id'] = $this->companyId;
        try {
            $this->model->transaction(function () use ($post)
            {
                $fields = [];
                $field  = [];
                for ($column = 1; $column <= $post['column_num']; $column++) {
                    for ($floor = 1; $floor <= $post['floor_num']; $floor++) {
                        $field['name']         = $post['name'] . '-' . $column . '-' . $floor;
                        $field['column_num']   = $column;
                        $field['floor_num']    = $floor;
                        $field['company_id']   = $post['company_id'];
                        $field['warehouse_id'] = $post['warehouse_id'];
                        $field['area_id']      = $post['area_id'];
                        $fields[]              = $field;
                    }
                }
                $this->model->saveAll($fields);
            });
        } catch (\Exception $e) {
            return $this->error('新增失败:' . $e->getMessage());
        }
        return $this->success('新增成功');
    }

    /**
     * @NodeAnnotation (title="新增")
     * @throws BaseException
     */
    public function addAll(Request $request): Json
    {
        $this->checkPost();
        $row = $request->post('row');
        if (!$row) return $this->error('数据提交错误');
        $row    = json_decode($row, true);
        $fields = array_map(function ($item)
        {
            $item['company_id'] = $this->companyId;
            return $item;
        }, $row);
        try {
            $this->model->transaction(function () use ($fields)
            {
                $this->model->saveAll($fields);
            });
        } catch (\Exception $e) {
            return $this->error('新增失败:' . $e->getMessage());
        }
        return $this->success('新增成功');
    }
}